/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

/** Allows the user to choose a profile */
final class Selection extends JDialog
{
    /** Language resource */
    private ResourceBundle msgBundle;

    private JPanel panel;

    /** List with existing profiles */
    private JComboBox comboBox;

    /** Profiles names */
    private String[] profiles;

    private JButton ok;
    private JButton create;

    /** Name of the selected (or created) profile */
    private String choice;

    private SelectionListener listener;

    /** Constructor
     *  @param msgBundle language resource
     */
    protected Selection(ResourceBundle msgBundle)
    {
        super((JFrame) null, msgBundle.getString("Selection.frameTitle"), true);

        this.msgBundle = msgBundle;

        choice = null;

        listener = new SelectionListener();

        panel = new JPanel();

        getProfiles();

        if (profiles != null)
            comboBox = new JComboBox(profiles);

        else
        {
            comboBox = new JComboBox();
            comboBox.setEnabled(false);
        }

        comboBox.setEditable(false);

        panel.add(comboBox);

        ok = new JButton("OK");

        if (profiles == null)
            ok.setEnabled(false);

        ok.addActionListener(listener);
        panel.add(ok);

        create = new JButton(msgBundle.getString("Selection.createLabel"));
        create.addActionListener(listener);
        panel.add(create);

        setContentPane(panel);

        addWindowListener(listener);

        setSize(300, 200);
    }

    /** Get the names of the existing profiles */
    private void getProfiles()
    {
        File f = new File(".");
        String files[] = f.list();

        int nb = 0;

        for (int i = 0; i < files.length; i++)
            if (files[i].endsWith(".prf"))
                nb++;

        if (nb > 0)
        {
            profiles = new String[nb];

            for (int i = 0, index = 0; i < files.length; i++)
            {
                if (files[i].endsWith(".prf"))
                {
                    profiles[index++] = files[i].substring(0, files[i].length() - 4);

                    if (index == nb)
                        break;
                }
            }
        }
    }

    /** Returns the selected profile name
     *  @return profile name
     */
    protected final String getChoice()
    {
        return (choice);
    }

    /** Listener for this class */
    private final class SelectionListener extends WindowAdapter implements ActionListener
    {
        /** Method called when the window is closed
         *  FIXME : we exit here because if the user closes this window when launching JMail, it does have to exit. But it the user wanna switch profile, this action has not to exit
         *  @param e window event triggered
         */
        public final void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }

        /** This method is invoked when an event is triggered
         *  @param e event
         */
        public final void actionPerformed(ActionEvent e)
        {
            JButton b = (JButton) e.getSource();

            if (b == ok)
            {
                choice = (String) comboBox.getSelectedItem();
                hide();
            }

            else if (b == create)
            {
                CreateProfile c = new CreateProfile(msgBundle);

                choice = c.getProfile();
                c.dispose();

                hide();
            }
        }
    }
}
